package com.example.forum2.Service.impl;

import com.example.forum2.Service.IArticleService;
import com.example.forum2.Service.IBoardService;
import com.example.forum2.Service.IUserService;
import com.example.forum2.comment.AppResult;
import com.example.forum2.comment.ResultCode;
import com.example.forum2.config.AppConfig;
import com.example.forum2.dao.ArticleMapper;
import com.example.forum2.exception.ApplicationExeception;
import com.example.forum2.model.Article;
import com.example.forum2.model.Board;
import com.example.forum2.model.User;
import com.example.forum2.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;

     @Autowired

    private IUserService userService;
    @Autowired
    private IBoardService boardService;
    @Autowired
    private IBoardServiceImpl iBoardService;

    @Override
    public void create(Article article) {


        if (article == null || article.getUserId() == null || article.getBoardId() == null
                || StringUtil.isEmpty(article.getTitle())
                || StringUtil.isEmpty(article.getContent())) {

            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }


        article.setVisitCount(0);
        article.setReplyCount(0);
        article.setLikeCount(0);
        article.setDeleteState((byte) 0);
        article.setState((byte) 0);
        Date date = new Date();
        article.setCreateTime(date);
        article.setUpdateTime(date);

        int articleRow = articleMapper.insertSelective(article);
        if (articleRow <= 0) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_CREATE));

        }


        User user = userService.selectById(article.getUserId());

        if (user == null) {
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_CREATE));

        }

        userService.addOneArticleCountById(user.getId());

        Board board = iBoardService.selectById(article.getBoardId());

        if (board == null) {
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_CREATE));
        }

        boardService.addOneArticleCountById(board.getId());

    }

    @Override
    public List<Article> selectAll() {
        return articleMapper.selectAll();
    }

    @Override
    public List<Article> selectAllByBoardId(Long boardId) {
        if(boardId==null||boardId<=0){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        Board board = boardService.selectById(boardId);
        if(board==null){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        List<Article> articles = articleMapper.selectAllByBoardId(boardId);
        return articles;
    }

    @Override
    public Article selectDetailById(Long id) {
        if(id==null||id<=0){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
        Article article = articleMapper.selectDetailById(id);
        if(article==null){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
        Article article1 = new Article();
        article1.setId(article.getId());
        article1.setVisitCount(article.getVisitCount()+1);
        int row = articleMapper.updateByPrimaryKeySelective(article1);
        if(row<0){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
        article.setVisitCount(article.getVisitCount()+1);
        return article;
    }

    @Override
    public void modify(Long id, String title, String content) {
        if (id == null || id <= 0 || StringUtil.isEmpty(title)||StringUtil.isEmpty(content)) {
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article updateArticle = new Article();
        updateArticle.setId(id);
        updateArticle.setTitle(title);
        updateArticle.setContent(content);
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row<=0){
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Article selectById(Long id) {
        if (id == null || id <= 0 ) {
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void thumbsUpById(Long id) {
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article==null){
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        if(article.getState()==1||article.getState()==1){
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        Article article1 = new Article();
        article1.setId(article.getId());
        article1.setLikeCount(article.getLikeCount()+1);
        article1.setUpdateTime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(article1);
        if(row!=1){
            log.warn(AppResult.failed().toString());
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public void deleteById(Long id) {
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
      //  article.setDeleteState((byte)0);
        if(article==null||article.getDeleteState()==1){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        Article updateArticke = new Article();
        updateArticke.setId(article.getId());
        updateArticke.setDeleteState((byte) 1);
        int row = articleMapper.updateByPrimaryKeySelective(updateArticke);
        if(row!=1){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
        boardService.subOneArticleCountByID(article.getBoardId());
        userService.subOneArtucleCountById(article.getUserId());
        log.info("删除帖子成功");

    }

    @Override
    public void addOneArticleCountById(Long id) {
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article==null||article.getDeleteState()==1){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        if(article.getState()==1){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setReplyCount(article.getReplyCount()+1);
        updateArticle.setUpdateTime(new Date());
       int row= articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row!=1){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
    }

    @Override

    public void addOneReplyCountById(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article == null || article.getDeleteState() == 1) {
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));

        }
        if (article.getState() == 1) {
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));

        }

        Article updateArticle = new Article();

        updateArticle.setId(article.getId());
        updateArticle.setReplyCount(article.getReplyCount() + 1);

        updateArticle.setUpdateTime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);

        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationExeception(AppResult.failed(ResultCode.ERROR_SERVICES));

        }

    }

    @Override
    public List<Article> selectByUserId(Long userId) {
        if(userId==null||userId<=0){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User user=userService.selectById(userId);
        if(user==null){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        List<Article> articles= articleMapper.selectByUserId(userId);
        return articles;
    }
}